﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Media;
using System.Xml.Serialization;
using System.Windows;

namespace Notepad_Clone
{
    public class NotepadCloneSettings
    {
        // 默认设置
        public WindowState WindowState = WindowState.Normal;
        public Rect RestoreBounds = Rect.Empty;
        public TextWrapping TextWrapping = TextWrapping.NoWrap;
        public string FontFamily = string.Empty;
        public string FontStyle = new FontStyleConverter().ConvertToString(FontStretches.Normal);
        public string FontWeight = new FontWeightConverter().ConvertToString(FontWeights.Normal);
        public string FontStretch = new FontStretchConverter().ConvertToString(FontStretches.Normal);
        public double FontSize = 11;

        /// <summary>
        /// 文件全名(含路径)
        /// </summary>
        /// <param name="strAppData"></param>
        /// <returns></returns>
        public virtual bool Save(string strAppData)
        {
            try
            {
                Directory.CreateDirectory(Path.GetDirectoryName(strAppData));
                StreamWriter write = new StreamWriter(strAppData);
                XmlSerializer xml = new XmlSerializer(this.GetType());
                xml.Serialize(write, this);
                write.Close();
            }
            catch
            {
                return false;
            }

            return true;
        }

        public static object Load(Type type, string strAppData)
        {
            StreamReader reader;
            object settings;
            XmlSerializer xml = new XmlSerializer(type);
            try
            {
                reader = new StreamReader(strAppData);
                settings = xml.Deserialize(reader);
                reader.Close();
            }
            catch
            {
                settings = type.GetConstructor(Type.EmptyTypes).Invoke(null);
            }

            return settings;
        }
    }
}
